package top.canyue.eca_manager.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import top.canyue.eca_manager.service.RedisService;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;


/**
 * @author canyue
 * @version 1.1。eca
 * &#064;date  2024/3/25 15:04
 * Spring Redis 转接件
 */
@Service
public class RedisServiceImpl implements RedisService {
    private static final Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);
    private static final String KEY_PREFIX = "eca:"; //key前缀
    private static final int TIME_OUT = 8;    //Key生命周期，单位：小时，计划后续使用application.yml文件配置
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private ValueOperations<String, Object> valueOperations;
    @Resource
    private HashOperations<String, String, Object> hashOperations;

    @Override
    public boolean set(String key, String value) {
        try {
            valueOperations.set(KEY_PREFIX + key, value);
            redisTemplate.expire(KEY_PREFIX + key, TIME_OUT, TimeUnit.HOURS);
            logger.info("redis set success, key:{},value:{}", KEY_PREFIX + key, value);
            return true;
        } catch (Exception e) {
            logger.error("redis set error, key:{}", KEY_PREFIX + key, e);
            return false;
        }
    }

    @Override
    public <T> T get(String key) {
        try {
            Object result = valueOperations.get(KEY_PREFIX + key);
            logger.info("redis get success, key:{}", KEY_PREFIX + key);
            return (T) result;
        } catch (Exception exception) {
            logger.error("redis set error, key:{}", KEY_PREFIX + key, exception);
            return null;
        }
    }

    @Override
    public boolean incrBy(String key, long increment) {
        try {
            Long result = valueOperations.increment(KEY_PREFIX + key, increment);
            redisTemplate.expire(KEY_PREFIX + key, TIME_OUT, TimeUnit.HOURS);
            logger.info("redis incrBy success, key:{},increment:{}", KEY_PREFIX + key, result);
            return true;
        } catch (Exception e) {
            logger.error("redis incrBy error, key:{}", KEY_PREFIX + key, e);
            return false;
        }
    }

    @Override
    public boolean incrBy(String key, double increment) {
        try {
            Double result = valueOperations.increment(KEY_PREFIX + key, increment);
            redisTemplate.expire(KEY_PREFIX + key, TIME_OUT, TimeUnit.HOURS);
            logger.info("redis incrBy success, key:{},increment:{}", KEY_PREFIX + key, result);
            return true;
        } catch (Exception e) {
            logger.error("redis incrBy error, key:{}", KEY_PREFIX + key, e);
            return false;
        }
    }

    @Override
    public boolean del(String key) {
        try {
            redisTemplate.delete(KEY_PREFIX + key);
            logger.info("redis del success, key:{}", KEY_PREFIX + key);
            return true;
        } catch (Exception e) {
            logger.error("redis del error, key:{}", KEY_PREFIX + key, e);
            return false;
        }
    }

    @Override
    public boolean hSet(String key, String item, String value) {
        try {
            hashOperations.put(KEY_PREFIX + key, item, value);
            redisTemplate.expire(KEY_PREFIX + key, TIME_OUT, TimeUnit.HOURS);
            logger.info("redis hSet success, key:{},item:{},value:{}", KEY_PREFIX + key, item, value);
            return true;
        } catch (Exception e) {
            logger.error("redis hSet error, key:{}", KEY_PREFIX + key, e);
            return false;
        }
    }

    @Override
    public <T> T hGet(String key, String item) {
        try {
            Object value = hashOperations.get(KEY_PREFIX + key, item);
            logger.info("redis hGet success, key:{},item:{},value:{}", KEY_PREFIX + key, item, value);
            return (T) value;
        } catch (Exception exception) {
            logger.error("redis hGet error, key:{}", KEY_PREFIX + key, exception);
            return null;
        }
    }

    @Override
    public boolean hDel(String key, String item) {
        try {
            hashOperations.delete(KEY_PREFIX + key, item);
            logger.info("redis hDel success, key:{},item:{}", KEY_PREFIX + key, item);
            return true;
        } catch (Exception exception) {
            logger.error("redis hDel error, key:{}", KEY_PREFIX + key, exception);
            return false;
        }
    }


    @Override
    public boolean hExists(String key, String item) {
        try {
            boolean result = hashOperations.hasKey(KEY_PREFIX + key, item);
            logger.info("redis hExists success, key:{},item:{},result:{}", KEY_PREFIX + key, item, result);
            return result;
        } catch (Exception e) {
            logger.error("redis hExists error, key:{}", KEY_PREFIX + key, e);
            return false;
        }
    }

    @Override
    public boolean hIncrBy(String key, String item, long increment) {
        try {
            Long result = hashOperations.increment(KEY_PREFIX + key, item, increment);
            redisTemplate.expire(KEY_PREFIX + key, TIME_OUT, TimeUnit.HOURS);
            logger.info("redis hIncrBy success, key:{},item:{},increment:{}", KEY_PREFIX + key, item, result);
            return true;
        } catch (Exception e) {
            logger.error("redis hIncrBy error, key:{}", KEY_PREFIX + key, e);
            return false;
        }
    }

    @Override
    public boolean hIncrBy(String key, String item, double increment) {
        try {
            double result = hashOperations.increment(KEY_PREFIX + key, item, increment);
            redisTemplate.expire(KEY_PREFIX + key, TIME_OUT, TimeUnit.HOURS);
            logger.info("redis hIncrBy success, key:{},item:{},increment:{}", KEY_PREFIX + key, item, result);
            return true;
        } catch (Exception e) {
            logger.error("redis hIncrBy error, key:{}", KEY_PREFIX + key, e);
            return false;
        }
    }

    @Override
    public Set<String> hKeys(String key) {
        try {
            Set<String> items = hashOperations.keys(KEY_PREFIX + key);
            logger.info("redis hKeys success, key:{},items:{}", KEY_PREFIX + key, items);
            return items;
        } catch (Exception e) {
            logger.error("redis hKeys error, key:{}", KEY_PREFIX + key, e);
            return null;
        }
    }

}
